//
//  UINavigationItem+CH.m
//  crm
//
//  Created by weacar on 2017/12/14.
//  Copyright © 2017年 weacar.com. All rights reserved.
//

#import "UINavigationItem+CH.h"

static const CGFloat iconHeight = 30;
static const CGFloat iconWidth = 30;

static void *clickblockKey = &clickblockKey;

typedef void(^buttonclickblock)(NSInteger indexPath);

@interface UINavigationItem ()


@property (nonatomic, strong)buttonclickblock ys_buttonclickblock;

@end

@implementation UINavigationItem (CH)

- (buttonclickblock)ys_buttonclickblock{
    return objc_getAssociatedObject(self, &clickblockKey);
}

- (void)setYs_buttonclickblock:(buttonclickblock)ys_buttonclickblock{
    return objc_setAssociatedObject(self, &clickblockKey, ys_buttonclickblock, OBJC_ASSOCIATION_COPY);
}

- (void)ys_itmeType:(ItemType)itemType itemNames:(NSArray *)itemNames firstItemSpaceWithScreen:(CGFloat)screenSpace itemSpace:(CGFloat)itemSpace handlerBlock:(void(^)(NSInteger indexPath))handlerBlock{
    if (itemType == LeftItemType) {
        self.leftBarButtonItems = [self creatItemWithImageNames:itemNames textColor:nil font:nil firstItemSpaceWithScreen:screenSpace itemSpace:itemSpace];
    }else{
        self.rightBarButtonItems = [self creatItemWithImageNames:itemNames textColor:nil font:nil firstItemSpaceWithScreen:screenSpace itemSpace:itemSpace];
    }
    self.ys_buttonclickblock = ^(NSInteger indexPath) {
        handlerBlock(indexPath);
    };
}

- (void)ys_itmeType:(ItemType)itemType itemNames:(NSArray *)itemNames textColor:(UIColor *)textColor font:(UIFont *)font firstItemSpaceWithScreen:(CGFloat)screenSpace itemSpace:(CGFloat)itemSpace handlerBlock:(void(^)(NSInteger indexPath))handlerBlock{
    if (itemType == LeftItemType) {
        self.leftBarButtonItems = [self creatItemWithImageNames:itemNames textColor:textColor font:font firstItemSpaceWithScreen:screenSpace itemSpace:itemSpace];
    }else{
        self.rightBarButtonItems = [self creatItemWithImageNames:itemNames textColor:textColor font:font firstItemSpaceWithScreen:screenSpace itemSpace:itemSpace];
    }
    self.ys_buttonclickblock = ^(NSInteger indexPath) {
        handlerBlock(indexPath);
    };
}

- (NSArray *)creatItemWithImageNames:(NSArray *)imageNames textColor:(UIColor *)textColor font:(UIFont *)font firstItemSpaceWithScreen:(CGFloat)screenSpace itemSpace:(CGFloat)itemSpace{
    NSMutableArray *items = [NSMutableArray array];
    for (int i = 0; i < imageNames.count; i++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, iconWidth, iconHeight)];
        button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
        [button setTitleColor:textColor forState:UIControlStateNormal];
        button.titleLabel.font = font;
        if ([UIImage imageNamed:imageNames[i]]) {
            [button setImage:[UIImage imageNamed:imageNames[i]] forState:UIControlStateNormal];
        }else{
            [button setTitle:imageNames[i] forState:UIControlStateNormal];
        }
        button.tag = i + 1000;
        button.contentMode = UIViewContentModeScaleAspectFit;
        [button addTarget:self action:@selector(itemBTAction:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        if (i == 0) {
            fixedSpace.width = screenSpace;
        }else{
            fixedSpace.width = itemSpace;
        }
        [items addObject:fixedSpace];
        [items addObject:buttonItem];
    }
    return items;
}

- (void)itemBTAction:(UIButton *)sender{
    self.ys_buttonclickblock(sender.tag - 1000);
}


@end
